"use client"
import { Button } from "@heroui/button";
import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, useDisclosure } from "@heroui/modal";
import { IoCloudDownloadOutline } from "react-icons/io5";
import { Image } from "@heroui/image";
import { QRCodeSVG } from 'qrcode.react';
import { FaRegBell } from "react-icons/fa6";
import { useRef, useState } from 'react';
import html2canvas from 'html2canvas';
import { MyResponse, request } from "@/lib/request";
import { quarkCookie, quarkPfid } from '@/config/common';

interface IProps {
    current?: any
}
export default function GetLink(props: IProps) {
    const { isOpen, onOpen, onOpenChange } = useDisclosure();
    const contentRef = useRef(null);
    const handleSaveAsImage = () => {
        if (contentRef.current) {
            html2canvas(contentRef.current).then((canvas) => {
                const dataUrl = canvas.toDataURL('image/png'); // 将 canvas 转为图片
                const link = document.createElement('a');
                link.href = dataUrl;
                link.download = '夸克4K.png'; // 设置下载文件名
                link.click();
            });
        }
    };
    const ownShareLink = useRef('')
    const [isLoading, setIsLoading] = useState(false);
    return (
        <>
            <Button color="danger" isLoading={isLoading} size="sm" onPress={async () => {
                if (props.current?.isOtherQuarkLink === '1') {
                    setIsLoading(true)
                    const result = await request<MyResponse>("/switch2OwnShareLink", "POST", {
                        url: props.current?.quarkLink,
                        // url: 'https://pan.quark.cn/s/7cb42f5566a6#/list/share',
                        pfid: quarkPfid,
                        cookie: quarkCookie
                    })
                    setIsLoading(false)
                    if (result?.code === 200) {
                        ownShareLink.current = result.data
                        onOpen()
                    }
                } else {
                    ownShareLink.current = props.current?.quarkLink
                    onOpen()
                }
            }} startContent={<IoCloudDownloadOutline size={16} />} variant="bordered">
                点击获取链接
            </Button>
            <Modal ref={contentRef} isOpen={isOpen} onOpenChange={onOpenChange}>
                <ModalContent>
                    {(onClose) => (
                        <>
                            <ModalHeader className="flex flex-row items-center gap-1">
                                <Image
                                    alt="夸克"
                                    src="/images/quark_icon.ico"
                                    width={20}
                                />
                                夸克
                            </ModalHeader>
                            <ModalBody>
                                <div className="flex flex-col justify-center items-center gap-4">
                                    <h2>资源[{props.current?.title} {props.current?.webdramaType}]获取成功</h2>
                                    <div>
                                        <QRCodeSVG value={ownShareLink.current} />
                                    </div>
                                    <div className="w-[80%] text-center">资源链接已经生成，为了方便使用请使用手机扫码领取转存</div>
                                    <div className="border-1 border-[#D4D4D8] rounded-lg p-4 text-xs">
                                        <div>
                                            <FaRegBell className="inline mr-2 mb-1" color="#F5A524" size={16} /><span className="text-[#F54180]">声明：</span>全站链接通过蜘蛛程序收集自网盘分享链接，以非人工方式自动生成，网站本身不储存、复制、传播、控制编辑任何网盘文件，也不提供下载服务，其链接跳转至官方网盘，文件的有效性和安全性需要您自行判断。
                                        </div>
                                        <div className="py-2">本站遵守相关法律法规，坚决杜绝一切违规不良信息，如您发现任何涉嫌违规的网盘信息，请立即向网盘官方网站举报</div>
                                        <div>本站作为非经营性网站，所有服务仅供学习交流使用，无任何收费请求，切勿上当支付。</div>
                                    </div>
                                </div>
                            </ModalBody>
                            <ModalFooter>
                                {/* <Button color="danger" size="sm" variant="light" onPress={onClose}>
                                    Close
                                </Button> */}
                                <Button color="primary" onPress={handleSaveAsImage} size="sm" >
                                    点击下载二维码分享
                                </Button>
                            </ModalFooter>
                        </>
                    )}
                </ModalContent>
            </Modal>
        </>
    );
}
